package top.hkyzf.study.leetcode.string;

import org.junit.Test;

/**
 * description:
 *
 * @author zhufeng
 * @version 1.0
 * @date 2024-3-17 11:01
 */
public class 统计以给定字符开头和结尾的子字符串总数_100236 {
    public long countSubstrings(String s, char c) {
        int count = 0;
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == c) {
                count++;
            }
        }
        return c_n_2(count) + count;
    }

    private long c_n_2(int n) {
        if (2 > n) {
            return 0;
        }
        return (long) n * (n - 1) / ((long) 2);
    }

    @Test
    public void testCountSubstrings () {
        String s = "tttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttwjgqljuntak";
        char c = 't';
        System.out.println(countSubstrings(s, c));
    }
}
